package com.code.learn.Ipersistance.sqlSession;

import com.code.learn.Ipersistance.config.Configuration;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.*;

/**
 * @author Nina
 * @create 2020/12/7 下午4:15
 * @desc 构建者模式
 * 注意：这里是没有Configuration作为私有属性的，Configuration和本类无直接的隶属关系
 */
public class SqlSessionFactoryBuilder {

    /**
     * 读取配置文件并整合到Configuration，
     * 获取SqlSessionFactory，并把configuration传入其中
     * @param inputStream
     * @return
     * @throws DocumentException
     * @throws PropertyVetoException
     */
    public SqlSessionFactory build(InputStream inputStream) throws DocumentException, PropertyVetoException {

        // 解析配置文件并输出到Configuration
        XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder();
        Configuration configuration = xmlConfigBuilder.buildConfig(inputStream);
        // 构建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);

        return sqlSessionFactory;
    }
}
